package eteam.aps.systemservice.service;

import cn.hutool.core.convert.Convert;
import com.github.xiaoymin.knife4j.annotations.ApiOperationSupport;
import eteam.aps.common.QueryResult;
import eteam.aps.common.R;
import eteam.aps.common.exceptions.AuthException;
import eteam.aps.systeminterface.model.dto.LogDto;
import eteam.aps.systeminterface.model.dto.VerifyTokenDto;
import eteam.aps.systeminterface.model.entity.LogEntity;
import eteam.aps.systeminterface.model.reqparam.IdParam;
import eteam.aps.systeminterface.model.reqparam.IdsParam;
import eteam.aps.systeminterface.model.reqparam.QueryLogParam;
import eteam.aps.systeminterface.service.ILogController;
import eteam.aps.systemservice.bll.LogBll;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.apache.ibatis.annotations.Insert;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;

/**
 * 日志服务
 */
@Api(tags = "日志服务控制器")
@RestController
//@RequestMapping(value = "/api/log")
public class LogController implements ILogController {
    @Autowired
    private HttpServletRequest request;
    @Autowired
    private LogBll logBll;

    /**
     * 分页查询日志
     *
     * @param requestDto
     * @return
     */
    @ApiOperationSupport(order = 1)
    @ApiOperation(value = "分页查询日志")
    @Override
    @GetMapping(value = "/api/log/querylogpagelist")
    public R<QueryResult<LogEntity>> queryLogPageList(@Validated QueryLogParam requestDto) throws Exception, AuthException {
        return R.success(logBll.queryPageList(requestDto));
    }

    /**
     * 记录日志
     *
     * @param logDto: 日志对象
     * @return
     */
    @ApiOperationSupport(order = 2, ignoreParameters = {"logDto.fid"})
    @ApiOperation(value = "记录日志")
    @Override
    @PostMapping(value = "/api/log/createlog")
    public R createLog(@Validated(Insert.class) @RequestBody LogDto logDto) throws Exception, AuthException {
        VerifyTokenDto tokenDto = Convert.convert(VerifyTokenDto.class, request.getAttribute("tokenInfo"));
        logBll.createLog(tokenDto, logDto);
        return R.success();
    }

    /**
     * 删除日志
     *
     * @param idParam
     * @return
     */
    @ApiOperationSupport(order = 3)
    @ApiOperation(value = "删除日志")
    @Override
    @PostMapping(value = "/api/log/deletelogbyid")
    public R deleteLogById(@Validated @RequestBody IdParam idParam) throws Exception, AuthException {
        logBll.deleteLog(idParam.getObjid());
        return R.success();
    }

    /**
     * 批量删除日志
     *
     * @param idsParam
     * @return
     */
    @ApiOperationSupport(order = 4)
    @ApiOperation(value = "批量删除日志")
    @Override
    @PostMapping(value = "/api/log/deletelogbyids")
    public R deleteLogByIds(@Validated @RequestBody IdsParam idsParam) throws Exception, AuthException {
        logBll.deleteLog(idsParam.getObjids());
        return R.success();
    }
}
